package DesginFotmat;

import lombok.extern.slf4j.Slf4j;

/**
 * 设计模式，顺序控制
 * 控制t1线程在t2线程运行后才运行
 */
@Slf4j
public class SequenceControl {
    static final Object lock = new Object();
    //t2是否运行过
    static boolean t2HasRunned = false;
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                synchronized(lock){
                    while(!t2HasRunned){
                        try{
                            log.debug("Waiting for lock unlock");
                            lock.wait();
                        }catch(InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                }
                log.debug("Thread 1");
            }
        },"t1");

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                synchronized(lock){
                    log.debug("Thread 2");
                    t2HasRunned = true;
                    lock.notify();
                }
            }
        },"t2");

        t1.start();
        t2.start();
    }
}
